<?php

namespace App\Model;

use Hyperf\Database\Model\Relations\BelongsTo;
use Hyperf\Database\Model\Relations\HasMany;
use Util\Common;


/**
 * App\Distributor
 *
 * @property int $id
 * @property int $distributor_id 业主id
 * @property float $fee 充满自停金额
 * @property int $minutes 分钟数
 * @property string $name 套餐名称
 * @property int $sort 排序
 * @property int $is_autoload 充满自停开启关闭
 * @property int $is_del 软删除
 * @property int $type 1=功率套餐 3=电量套餐
 * @property float $service_fee 服务费单价/每度
 * @property int $service_type 服务费计费方式 1=按每度 2=按每小时
 * @property float $min_price 最低消费
 * @property float $elec_price 每度/电价
 */


class ChargeOption extends Model
{
//    use TimeZoneTrait;

    public bool $timestamps = true;

    protected ?string $table = 'charge_options';


    protected array $guarded = [];

    protected array $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
    ];

    /**经销商
     * @return BelongsTo
     */
    public function distributor():BelongsTo{
        return $this->belongsTo(Distributors::class,'distributor_id','id');
    }

    public function device():HasMany{
        return $this->hasMany(Device::class,'charge_id','id');

    }

    public function stations():HasMany{
        return $this->hasMany(Station::class,'charge_id','id');
    }


    /**套餐金额详情
     * @return HasMany
     */
    public function time_children():HasMany{
        return $this->hasMany(ChargeDetail::class,'charge_option_id','id')
            ->where(function ($query) {
                $query->where('type',2);
            })
//            ->where('is_del', 1)
            ->selectRaw('*, ROUND(fee, 2) as fee');
    }


    /**功率分档详情
     * @return HasMany
     */
    public function power_children():HasMany{
        return $this->hasMany(ChargeDetail::class,'charge_option_id','id')
            ->where(function ($query) {
                $query->where('type',1);
            });
//            ->where('is_del', 1);
    }

    /**服务费功率分档详情
     * @return HasMany
     */
    public function service_children():HasMany{
        return $this->hasMany(ChargeDetail::class,'charge_option_id','id')
            ->where(function ($query) {
                $query->where('type', 3);
            });
   //         ->where('is_del', 1);
    }


}
